package com.jiamao.goplay.ui.user;

import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.UserInvitationLayoutBinding;
import com.jiamao.goplay.ui.user.vm.UserInvitationVM;
import com.jiamao.goplay.util.StringUtils;
import com.netease.nim.uikit.api.NimUIKit;
import com.netease.nimlib.sdk.util.NIMUtil;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;


/**
 * 邀请我的
 */
public class UserInvitationFragment extends BaseMVVMFragment<UserInvitationLayoutBinding, UserInvitationVM> {



    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.user_invitation_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public UserInvitationVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UserInvitationVM.class);
    }
    @Override
    public void initViewObservable() {
        viewModel.uc.refreshEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                binding.refreshLayout.finishRefresh();
                binding.refreshLayout.finishLoadMore();
            }
        });
        viewModel.uc.refreshNums.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                TextView titleTv =  binding.getRoot().findViewById(R.id.title_right);
                titleTv.setText("共"+ StringUtils.formatBigNum(aBoolean)+"个邀请");
            }
        });
        viewModel.uc.startChat.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String aBoolean) {

                NimUIKit.startP2PSession(getActivity(),aBoolean);
            }
        });
        super.initViewObservable();
    }

    @Override
    public void initData() {
        super.initData();


        TextView tv=(TextView)binding.getRoot().findViewById(R.id.title_right);
        String str="共0个邀请";
        tv.setText(Html.fromHtml(str));


        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();
        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
        titleTv.setText("邀请我");
        binding.getRoot().findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getDataList(0);
            }
        });
        binding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.getDataList(1);
            }
        });

       // binding.userGotoListview.setEmptyView(binding.idEmptyView);
        binding.refreshLayout.autoRefresh();
    }
}
